package com.zyw.weibo.ui.support;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.emagsoftware.ui.adapterview.DataHolder;
import cn.emagsoftware.ui.adapterview.ViewHolder;
import cn.emagsoftware.util.DateUtilities;

import com.zyw.weibo.Utilities;
import com.zyw.weibo.data.Comment;
import com.zyw.weibo.ui.R;

public class CommentDataHolder extends DataHolder {
	
	public CommentDataHolder(Comment cm){
		super(cm, 0);
	}
	
	@Override
	public View onCreateView(Context arg0, int arg1, Object arg2) {
		// TODO Auto-generated method stub
		LinearLayout wrap = (LinearLayout)LayoutInflater.from(arg0).inflate(R.layout.commentlist_list_item, null);
		TextView name = (TextView)wrap.findViewById(R.id.tvCommentListName);
		TextView time = (TextView)wrap.findViewById(R.id.tvCommentListTime);
		TextView content = (TextView)wrap.findViewById(R.id.tvCommentListContent);
		ViewHolder viewHolder = new ViewHolder(name,time,content);
		wrap.setTag(viewHolder);
		onUpdateView(arg0,arg1,wrap,arg2,false);
		return wrap;
	}
	
	@Override
	public void onUpdateView(final Context arg0, int arg1, View arg2, Object arg3, boolean arg4) {
		// TODO Auto-generated method stub
		Comment cm = (Comment)arg3;
		View[] views = ((ViewHolder)arg2.getTag()).getParams();
		TextView name = (TextView)views[0];
		TextView time = (TextView)views[1];
		TextView content = (TextView)views[2];
		name.setText(cm.commentorid.nickname);
		time.setText(DateUtilities.getFormatDate(DateUtilities.getParseDate(cm.commentdate, "yyyy-MM-dd HH:mm:ss"), "MM-dd HH:mm"));
		content.setText(Html.fromHtml(Utilities.convertWeiboToHtmlStyle(cm.commentcontent),new Html.ImageGetter() {
			@Override
			public Drawable getDrawable(String source) {
				// TODO Auto-generated method stub
				int drawableId = arg0.getResources().getIdentifier(source, "drawable", arg0.getPackageName());
				Drawable d = arg0.getResources().getDrawable(drawableId);
				d.setBounds(0, -10, d.getIntrinsicWidth()+12,d.getIntrinsicHeight()+2);
				return d;
			}
		},null));
		
	}
	
}
